﻿namespace UI
{
    using System;
    using System.Net;
    using System.Windows.Forms;
    using XMLOut;
    using YculOut;
    using RSSOut;
    using System.Xml.Serialization;
    using System.IO;

    public partial class Form_main : Form
    {
        private CookieContainer cookie = null;

        private Blog blog;

        private IOut iout;

        public Form_main()
        {
            InitializeComponent();
            webBrowser_ycoolWebBrowser.ScriptErrorsSuppressed = true;
            Blog.CheckTime = 3;
            HTMLRequest.RetryTimes = 3;
            comboBox_OutType.SelectedIndex = 0;
            iout = new XmlOut(null);
            webBrowser_ycoolWebBrowser.Url = new Uri("http://www.ycool.com");
            webBrowser_ycoolWebBrowser.Refresh();            
        }

        private void button_GetPost_Click(object sender, EventArgs e)
        {
            this.button_GetPosts.Enabled = false;
            this.Visible = false;
            this.cookie = HTMLRequest.GetCookie(webBrowser_ycoolWebBrowser, textBox_domain.Text.Substring(7, textBox_domain.Text.Length - 7));
            this.blog = new Blog(textBox_domain.Text, this.cookie);            
            MessageBox.Show(string.Format("Done {0} posts", this.blog.PostCount));
            textBox_SavePath.Enabled =
                button_ChooseSavePath.Enabled =
                button_SaveXml.Enabled = this.blog.PostCount > 0 ? true : false;
            this.button_GetPosts.Enabled = true;
            this.Visible = true;
        }

        private void button_Save_Click(object sender, EventArgs e)
        {
            if (comboBox_OutType.SelectedIndex == 0)
            {
                this.iout = new XmlOut(this.blog);
            }
            else if (comboBox_OutType.SelectedIndex == 1)
            {
                this.iout = new RssOut(this.blog,false);
            }
            else if (comboBox_OutType.SelectedIndex == 2)
            {
                this.iout = new RssOut(this.blog,true);
            }
          
            XmlSerializer serializer = null;
            StreamWriter writer = null;
            try
            {
                writer = new StreamWriter("a.xml", false);
                serializer = new XmlSerializer(this.blog.GetType());
                serializer.Serialize(writer, this.blog);
            }
            catch
            {
                ;
            }

            finally
            {
                if (writer != null)
                {
                    writer.Close();
                }
            }
            this.iout.Out(textBox_SavePath.Text);
        }

        private void button_ChoosePath_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.FileName = textBox_domain.Text.Split('.')[0].Substring(7);
            saveFileDialog.Filter =string.Format("{0} files (*.{0})|*.{0}",this.iout.Extension());
            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                textBox_SavePath.Text = saveFileDialog.FileName;
            }
        }

        private void UpdateSavePath(string domain)
        {
            textBox_SavePath.Text = string.Format("{0}\\{1}.{2}",
                System.Environment.CurrentDirectory,
                domain,
                this.iout.Extension());
        }

        private void button_Go_Click(object sender, EventArgs e)
        {
            webBrowser_ycoolWebBrowser.Stop();
            webBrowser_ycoolWebBrowser.Url = new Uri(textBox_WebPath.Text);
            webBrowser_ycoolWebBrowser.Refresh();
        }

        private void button_YcoolHome_Click(object sender, EventArgs e)
        {
            webBrowser_ycoolWebBrowser.Url = new Uri("http://www.ycool.com");
            textBox_WebPath.Text = "http://www.ycool.com";
            webBrowser_ycoolWebBrowser.Refresh();
        }

        private void textBox_WebPath_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                button_Go_Click(sender, e);
            }
        }

        private void webBrowser_ycoolWebBrowser_Navigated(object sender, WebBrowserNavigatedEventArgs e)
        {
            
                textBox_WebPath.Text = webBrowser_ycoolWebBrowser.Url.ToString();
                if (textBox_WebPath.Text.Contains(".ycool.com"))
                {
                    textBox_domain.Text = string.Format("http://{0}.ycool.com", textBox_WebPath.Text.Split('.')[0].Substring(7));
                    UpdateSavePath(textBox_domain.Text.Split('.')[0].Substring(7));
                }
        }

        private void comboBox_OutType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox_OutType.SelectedIndex == 0)
            {
                this.iout = new XmlOut(this.blog);
            }
            else if(comboBox_OutType.SelectedIndex == 1)
            {
                this.iout = new RssOut(this.blog,false);
            }
            else if (comboBox_OutType.SelectedIndex == 2)
            {
                this.iout = new RssOut(this.blog,true);
            }
        }
    }
}
